package com.qdxwx.data.service.website;


import com.alibaba.fastjson2.JSON;
import com.qdxwx.common.cache.CacheUtils;
import com.qdxwx.common.constants.CacheConstants;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.website.WebsiteProfileMapper;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.website.WebsiteProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 网站管理配置service
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class WebsiteProfileServiceImpl implements WebsiteProfileService {

	private static final Logger logger = LoggerFactory.getLogger(WebsiteProfileServiceImpl.class);

	@Autowired
	private WebsiteProfileMapper websiteProfileMapper;


	@Override
	public int update(WebsiteProfile websiteProfile) {
		int row = websiteProfileMapper.updateWebsiteProfile(websiteProfile);
		if (row > 0) {
			CacheUtils.remove(CacheConstants.WEBSITE_PROFILE_PREFIX + websiteProfile.getType());
		}
		return row;
	}

	@Override
	public int insert(WebsiteProfile websiteProfile) {
		return websiteProfileMapper.insertWebsiteProfile(websiteProfile);
	}

	@Override
	public WebsiteProfile get(String typeStr) {
		return websiteProfileMapper.getWebsiteProfileByType(typeStr);
	}

	/**
	 * 根据type字符串查询网站配置
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getMap(String typeStr) {
		//根据类型获得数据 从cache获取
		String description = getString(typeStr);
		if (description == null) return new HashMap<>();
		try {
			//把json数据转化为Map
			return JSON.parseObject(description, Map.class);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return new HashMap<>();
		}
	}

	/**
	 * 根据type查询网站配置
	 */
	@Override
	public Map<String, Object> getMap(WebsiteProfileType type) {
		return getMap(type.toString());
	}

	@Override
	public Map<String, Object> getAlipay() {
		// 获得支付配置
		return getMap(WebsiteProfileType.alipay.toString());
		//map.get("payKey").toString();// 支付宝key
		//map.get("partnerId").toString();// 支付宝合作伙伴id
		//map.get("sellerEmail").toString();// 商家
	}

	@Override
	public Map<String, Object> getWxpay() {
		//获得微信支付配置
		return getMap(WebsiteProfileType.wxPay.toString());
	}

	@Override
	public String getString(String typeStr) {
		//根据类型获得数据 从cache获取
		String description = (String) CacheUtils.get(CacheConstants.WEBSITE_PROFILE_PREFIX + typeStr);
		if (description == null) {
			//cache为空查询数据库
			WebsiteProfile websiteProfile = websiteProfileMapper.getWebsiteProfileByType(typeStr);
			if (websiteProfile == null) {
				return null;
			}
			description = websiteProfile.getDescription();
			CacheUtils.set(CacheConstants.WEBSITE_PROFILE_PREFIX + typeStr, description, CacheConstants.WEBSITE_PROFILE_TIME);//设置key 时间一天
		}
		return description;
	}

	/**
	 * 根据type查询网站配置
	 */
	@Override
	public String getString(WebsiteProfileType type) {
		return getString(type.toString());
	}

	//是否包括敏感题
	@Override
	public boolean hasBadWord(String text) {
		String config = getString(WebsiteProfileType.sensitiveWords);
		if (TKString.isEmpty(config)) return false;
		String[] badWords = config.split(",");
		for (String word : badWords) {
			if (text.contains(word)) {
				return true;
			}
		}
		return false;
	}

	//检测敏感题(返回存在的第一个敏感题，没有则null)
	@Override
	public String checkBadWord(String text) {
		String config = getString(WebsiteProfileType.sensitiveWords);
		if (TKString.isEmpty(config)) return null;
		String[] badWords = config.split(",");
		for (String word : badWords) {
			if (text.contains(word)) {
				return word;
			}
		}
		return null;
	}

	@Override
	public String replaceBadWords(String text) {
		return replaceBadWords(text, "*");
	}

	@Override
	public String replaceBadWords(String text, String newStr) {
		String config = getString(WebsiteProfileType.sensitiveWords);
		if (TKString.isEmpty(config)) return text;
		String[] badWords = config.split(",");
		for (String word : badWords) {
			if (text.contains(word)) {
				text = text.replace(word, newStr);
			}
		}
		return text;
	}
}